package com.bootdo.market.service.impl;

import com.bootdo.market.dao.CommonDao;
import com.bootdo.market.dao.ProductCatalogDao;
import com.bootdo.market.domain.ProductArticleDO;
import com.bootdo.market.domain.ProductCatalogDO;
import com.bootdo.market.service.ProductCatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class ProductCatalogServiceImpl implements ProductCatalogService {
    @Autowired
    private ProductCatalogDao productCatalogDao;
    @Autowired
    private CommonDao commonDao;

    @Override
    public ProductCatalogDO get(Integer id) {
        return productCatalogDao.get(id);
    }

    @Override
    public List<ProductCatalogDO> list(Map<String, Object> map) {
        return productCatalogDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return productCatalogDao.count(map);
    }

    @Override
    public int save(ProductCatalogDO productCatalog) {
        return productCatalogDao.save(productCatalog);
    }

    @Override
    public int update(ProductCatalogDO productCatalog) {
        return productCatalogDao.update(productCatalog);
    }

    @Override
    public int remove(Integer id) {
        return productCatalogDao.remove(id);
    }

    @Override
    public int batchRemove(Integer[] ids) {
        return productCatalogDao.batchRemove(ids);
    }

    @Override
    public int saveProductCatalogTags(ProductCatalogDO productCatalog) {

        if (productCatalog.getTagid() != null && !"".equals(productCatalog.getTagid())) {
            String[] strings = productCatalog.getTagid().split(",");
            Arrays.asList(strings).stream().forEach(s -> {
                Map<String, Object> stringStringMap = new HashMap<>();
                stringStringMap.put("catalogId", productCatalog.getId());
                stringStringMap.put("tagId", s);
                commonDao.saveProductCatalogTags(stringStringMap);
            });
            return 1;
        }
        return 0;
    }

    @Override
    public int delProductCatalogTags(ProductCatalogDO productCatalog) {
        commonDao.delProductCatalogTags(productCatalog.getId());
        return 1;
    }

    @Override
    public List<ProductArticleDO> catalogArticleList(Map<String, Object> map) {
        return productCatalogDao.catalogArticleList(map);
    }

    @Override
    public int catalogArticleListCount(Map<String, Object> map) {
        return productCatalogDao.catalogArticleListCount(map);
    }
}
